<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="组件名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入组件名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品标识" prop="productKey">
        <el-input
          v-model="queryParams.productKey"
          placeholder="请输入产品标识"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运行状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择运行状态" style="width: 200px" clearable>
          <el-option label="stopped" value="stopped" />
          <el-option label="running" value="running" />
          <el-option label="running" value="connecting" />
        </el-select>
      </el-form-item>
      <el-form-item label="通讯协议" prop="protocol">
        <el-input
          v-model="queryParams.protocol"
          placeholder="请输入通讯协议"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组件状态" prop="enableFlag">
        <el-select v-model="queryParams.enableFlag" placeholder="请选择组件状态" style="width: 200px" clearable>
          <el-option label="开启" value="1" />
          <el-option label="关闭" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['manager:protocolComponent:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="protocolComponentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" width="50"/>
      <el-table-column label="组件名称" align="center" prop="name" width="120"/>
      <el-table-column label="产品标识" align="center" prop="productKey" width="120" />
      <el-table-column label="组件配置内容" align="center" prop="protocolConfig" width="300" :show-overflow-tooltip="true" />
      <el-table-column label="运行状态" align="center" prop="state"  width="100" />
      <el-table-column label="通讯协议" align="center" prop="protocol" />
      <el-table-column label="协议类别" align="center" prop="protocolType" />
      <el-table-column label="转换脚本" align="center" prop="converter" width="200" />
      <el-table-column label="组件状态" align="center" prop="enableFlag">
        <template #default="scope">
          <el-tag v-if="scope.row.enableFlag === 1" type="success">开启</el-tag>
          <el-tag v-else-if="scope.row.enableFlag === 0" type="danger">关闭</el-tag>
          <el-tag v-else type="info">未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  width="150">
        <template #default="scope">
          <el-button
            link type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['manager:protocolComponent:edit']"
          >修改</el-button>
          <el-button
            link type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['manager:protocolComponent:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改协议管理对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="protocolComponentRef" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12"></el-col>
          <el-col :span="12"></el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="产品标识" prop="productKey">
              <el-input v-model="form.productKey" placeholder="请选择产品标识" readonly >
                <template #append>
                  <el-button icon="Search" @click="selectProduct" />
                </template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="组件名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入组件名称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="组件属性配置" prop="protocolConfig">
          <el-input v-model="form.protocolConfig" readonly placeholder="请输入内容">
            <template #append>
              <el-button icon="Setting" @click="configComponent(form)" />
            </template>
          </el-input>
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="通讯协议" prop="protocol">
              <el-input v-model="form.protocol" placeholder="请输入通讯协议" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="转换脚本" prop="converter">
              <el-input v-model="form.converter" placeholder="请输入转换脚本" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="组件状态" prop="enableFlag">
          <el-radio-group v-model="form.enableFlag">
            <el-radio :label="1">开启</el-radio>
            <el-radio :label="0">关闭</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <select-product-info :open="openProduct"
                         @setProductKey="setProductKey"
                         @closeProduct="closeProduct">
    </select-product-info>
    <component-config :open="openConfig"
                      :productId="componentId"
                      :config="config"
                      @setConfig="setConfig"
                      @closeConfig="closeConfig"></component-config>
  </div>
</template>

<script setup name="ProtocolComponent">
import { getProtocolComponent, listProtocolComponent, addProtocolComponent, updateProtocolComponent, delProtocolComponent } from "@/api/manager/protocolComponent";
import ComponentConfig from './config'
import SelectProductInfo from './product'
const { proxy } = getCurrentInstance();

const protocolComponentList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const openProduct = ref(false)
const openConfig = ref(false)
const componentId = ref(0)
const config = ref(null)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    productKey: null,
    state: null,
    protocol: null,
    protocolType: null,
    converType: null,
    converter: null,
    enableFlag: null,
  },
  rules: {
    name: [
      { required: true, message: "组件名称不能为空", trigger: "blur" }
    ],
    productKey: [
      { required: true, message: "产品标识不能为空", trigger: "blur" }
    ],
    protocolConfig: [
      { required: true, message: "协议配置json内容不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询协议管理列表 */
function getList() {
  loading.value = true;
  listProtocolComponent(queryParams.value).then(response => {
    protocolComponentList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    productKey: null,
    protocolConfig: null,
    state: null,
    protocol: null,
    protocolType: null,
    converType: null,
    converter: null,
    transparent: null,
    enableFlag: 1,
    deleted: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("protocolComponentRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加组件";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getProtocolComponent(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改组件";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["protocolComponentRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateProtocolComponent(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addProtocolComponent(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除协议管理编号为"' + _ids + '"的数据项？').then(function() {
    return delProtocolComponent(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('protocolComponent/export', {
    ...queryParams.value
  }, `protocolComponent_${new Date().getTime()}.xlsx`)
}

function selectProduct(){
  openProduct.value = true
}

function setProductKey(productKey){
  form.value.productKey = productKey
  closeProduct()
}

function closeProduct(){
  openProduct.value = false
}

function configComponent(item){
  openConfig.value = true
  componentId.value = item.id
  config.value = item.protocolConfig
}

function setConfig(childConfig){
  form.value.protocolConfig = childConfig
  closeConfig()
}

function closeConfig(){
  openConfig.value = false
  componentId.value = 0
  config.value = ''
}

getList();
</script>
